<!DOCTYPE HTML>
<html lang="en" class="sidebar-visible no-js light">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>part3_02_cache-declarations - learning-gem5</title>
        <!-- Custom HTML head -->
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        <link rel="icon" href="../favicon.svg">
        <link rel="shortcut icon" href="../favicon.png">
        <link rel="stylesheet" href="../css/variables.css">
        <link rel="stylesheet" href="../css/general.css">
        <link rel="stylesheet" href="../css/chrome.css">
        <link rel="stylesheet" href="../css/print.css" media="print">
        <!-- Fonts -->
        <link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="../fonts/fonts.css">
        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="../highlight.css">
        <link rel="stylesheet" href="../tomorrow-night.css">
        <link rel="stylesheet" href="../ayu-highlight.css">

        <!-- Custom theme stylesheets -->
    </head>
    <body>
        <!-- Provide site root to javascript -->
        <script type="text/javascript">
            var path_to_root = "../";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script type="text/javascript">
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script type="text/javascript">
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add('js');
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script type="text/javascript">
            var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded affix "><a href="../part0_introduction.html">Learning gem-5</a></li><li class="chapter-item expanded "><a href="../part0_introduction.html"><strong aria-hidden="true">1.</strong> part0_introduction</a></li><li class="chapter-item expanded "><a href="../part1/part1_1_building.html"><strong aria-hidden="true">2.</strong> part1</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../part1/part1_1_building.html"><strong aria-hidden="true">2.1.</strong> part1_1_building</a></li><li class="chapter-item expanded "><a href="../part1/part1_2_simple_config.html"><strong aria-hidden="true">2.2.</strong> part1_2_simple_config</a></li><li class="chapter-item expanded "><a href="../part1/part1_3_cache_config.html"><strong aria-hidden="true">2.3.</strong> part1_3_cache_config</a></li><li class="chapter-item expanded "><a href="../part1/part1_4_gem5_stats.html"><strong aria-hidden="true">2.4.</strong> part1_4_gem5_stats</a></li><li class="chapter-item expanded "><a href="../part1/part1_5_gem5_example_configs.html"><strong aria-hidden="true">2.5.</strong> part1_5_gem5_example_configs</a></li><li class="chapter-item expanded "><a href="../part1/part1_6_extending_configs.html"><strong aria-hidden="true">2.6.</strong> part1_6_extending_configs</a></li></ol></li><li class="chapter-item expanded "><a href="../part2/part2_0_environment.html"><strong aria-hidden="true">3.</strong> part2</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../part2/part2_0_environment.html"><strong aria-hidden="true">3.1.</strong> part2_0_environment</a></li><li class="chapter-item expanded "><a href="../part2/part2_1_helloobject.html"><strong aria-hidden="true">3.2.</strong> part2_1_helloobject</a></li><li class="chapter-item expanded "><a href="../part2/part2_2_debugging.html"><strong aria-hidden="true">3.3.</strong> part2_2_debugging</a></li><li class="chapter-item expanded "><a href="../part2/part2_3_events.html"><strong aria-hidden="true">3.4.</strong> part2_3_events</a></li><li class="chapter-item expanded "><a href="../part2/part2_4_parameters.html"><strong aria-hidden="true">3.5.</strong> part2_4_parameters</a></li><li class="chapter-item expanded "><a href="../part2/part2_5_memoryobject.html"><strong aria-hidden="true">3.6.</strong> part2_5_memoryobject</a></li><li class="chapter-item expanded "><a href="../part2/part2_6_simplecache.html"><strong aria-hidden="true">3.7.</strong> part2_6_simplecache</a></li><li class="chapter-item expanded "><a href="../part2/part2_7_arm_power_modelling.html"><strong aria-hidden="true">3.8.</strong> part2_7_arm_power_modelling</a></li><li class="chapter-item expanded "><a href="../part2/part2_8_arm_dvfs_support.html"><strong aria-hidden="true">3.9.</strong> part2_8_arm_dvfs_support</a></li></ol></li><li class="chapter-item expanded "><a href="../part3/part3_00_MSIntro.html"><strong aria-hidden="true">4.</strong> part3</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../part3/part3_00_MSIntro.html"><strong aria-hidden="true">4.1.</strong> part3_00_MSIntro</a></li><li class="chapter-item expanded "><a href="../part3/part3_01_cache-intro.html"><strong aria-hidden="true">4.2.</strong> part3_01_cache-intro</a></li><li class="chapter-item expanded "><a href="../part3/part3_02_cache-declarations.html" class="active"><strong aria-hidden="true">4.3.</strong> part3_02_cache-declarations</a></li><li class="chapter-item expanded "><a href="../part3/part3_03_cache-in-ports.html"><strong aria-hidden="true">4.4.</strong> part3_03_cache-in-ports</a></li><li class="chapter-item expanded "><a href="../part3/part3_04_cache_actions.html"><strong aria-hidden="true">4.5.</strong> part3_04_cache_actions</a></li><li class="chapter-item expanded "><a href="../part3/part3_05_cache_transitions.html"><strong aria-hidden="true">4.6.</strong> part3_05_cache_transitions</a></li><li class="chapter-item expanded "><a href="../part3/part3_06_directory.html"><strong aria-hidden="true">4.7.</strong> part3_06_directory</a></li><li class="chapter-item expanded "><a href="../part3/part3_07_MSIbuilding.html"><strong aria-hidden="true">4.8.</strong> part3_07_MSIbuilding</a></li><li class="chapter-item expanded "><a href="../part3/part3_08_configuration.html"><strong aria-hidden="true">4.9.</strong> part3_08_configuration</a></li><li class="chapter-item expanded "><a href="../part3/part3_09_running.html"><strong aria-hidden="true">4.10.</strong> part3_09_running</a></li><li class="chapter-item expanded "><a href="../part3/part3_10_MSIdebugging.html"><strong aria-hidden="true">4.11.</strong> part3_10_MSIdebugging</a></li><li class="chapter-item expanded "><a href="../part3/part3_11_simple-MI_example.html"><strong aria-hidden="true">4.12.</strong> part3_11_simple-MI_example</a></li></ol></li><li class="chapter-item expanded "><a href="../part4_gem5_101.html"><strong aria-hidden="true">5.</strong> part4_gem5_101</a></li><li class="chapter-item expanded "><a href="../http://doxygen.gem5.org/develop/index.html"><strong aria-hidden="true">6.</strong> part4_gem5_102</a></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky bordered">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">learning-gem5</h1>

                    <div class="right-buttons">
                        <a href="../print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>
                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>
                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script type="text/javascript">
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <hr />
<h2>layout: documentation
title: Declaring a state machine
doc: Learning gem5
parent: part3
permalink: /documentation/learning_gem5/part3/cache-declarations/
author: Jason Lowe-Power</h2>
<h1 id="declaring-a-state-machine"><a class="header" href="#declaring-a-state-machine">Declaring a state machine</a></h1>
<p>Let's start on our first state machine file! First, we will create the
L1 cache controller for our MSI protocol.</p>
<p>Create a file called <code>MSI-cache.sm</code> and the following code declares the
state machine.</p>
<pre><code class="language-cpp">machine(MachineType:L1Cache, &quot;MSI cache&quot;)
    : &lt;parameters&gt;
{
    &lt;All state machine code&gt;
}
</code></pre>
<p>The first thing you'll notice about the state machine code is that is
looks very C++-like. The state machine file is like creating a C++
object in a header file, if you included all of the code there as well.
When in doubt, C++ syntax with <em>probably</em> work in SLICC. However, there
are many cases where C++ syntax is incorrect syntax for SLICC as well as
cases where SLICC extends the syntax.</p>
<p>With <code>MachineType:L1Cache</code>, we are naming this state machine <code>L1Cache</code>.
SLICC will generate many different objects for us from the state machine
using that name. For instance, once this file is compiled, there will be
a new SimObject: <code>L1Cache_Controller</code> that is the cache controller. Also
included in this declaration is a description of this state machine:
&quot;MSI cache&quot;.</p>
<p>There are many cases in SLICC where you must include a description to go
along with the variable. The reason for this is that SLICC was
originally designed to just describe, not implement, coherence
protocols. Today, these extra descriptions serve two purposes. First,
they act as comments on what the author intended each variable, or
state, or event, to be used for. Second, many of them are still exported
into HTML when building the HTML tables for the SLICC protocol. Thus,
while browsing the HTML table, you can see the more detailed comments
from the author of the protocol. It is important to be clear with these
descriptions since coherence protocols can get quite complicated.</p>
<h2 id="state-machine-parameters"><a class="header" href="#state-machine-parameters">State machine parameters</a></h2>
<p>Proceeding the <code>machine()</code> declaration is a colon, after which all of
the parameters to the state machine are declared. These parameters are
directly exported to the SimObject that is generated by the state
machine.</p>
<p>For our MSI L1 cache, we have the following parameters:</p>
<pre><code class="language-cpp">machine(MachineType:L1Cache, &quot;MSI cache&quot;)
: Sequencer *sequencer;
  CacheMemory *cacheMemory;
  bool send_evictions;

  &lt;Message buffer declarations&gt;

  {

  }
</code></pre>
<p>First, we have a <code>Sequencer</code>. This is a special class that is
implemented in Ruby to interface with the rest of gem5. The Sequencer is
a gem5 <code>MemObject</code> with a slave port so it can accept memory requests
from other objects. The sequencer accepts requests from a CPU (or other
master port) and converts the gem5 the packet into a <code>RubyRequest</code>.
Finally, the <code>RubyRequest</code> is pushed onto the <code>mandatoryQueue</code> of the
state machine. We will revisit the <code>mandatoryQueue</code> in
the <a href="../cache-in-ports">in-port section</a>.</p>
<p>Next, there is a <code>CacheMemory</code> object. This is what holds the cache data
(i.e., cache entries). The exact implementation, size, etc. is
configurable at runtime.</p>
<p>Finally, we can specify any other parameters we would like, similar to a
general <code>SimObject</code>. In this case, we have a boolean variable
<code>send_evictions</code>. This is used for out-of-order core models to notify
the load-store queue if an address is evicted after a load to squash a
load if it is speculative.</p>
<p>Next, also in the parameter block (i.e., before the first open bracket),
we need to declare all of the message buffers that this state machine
will use. Message buffers are the interface between the state machine
and the Ruby network. Messages are sent and received via the message
buffers. Thus, for each virtual channel in our protocol we need a
separate message buffer.</p>
<p>The MSI protocol needs three different virtual networks. Virtual
networks are needed to prevent deadlock (e.g., it is bad if a response
gets stuck behind a stalled request). In this protocol, the highest
priority is responses (virtual network 2), followed by forwarded
requests (virtual network 1), then requests have the lowest priority
(virtual network 0). See Sorin et al. for details on why these three
virtual networks are needed.</p>
<p>The following code declares all of the needed message buffers.</p>
<pre><code class="language-cpp">machine(MachineType:L1Cache, &quot;MSI cache&quot;)
: Sequencer *sequencer;
  CacheMemory *cacheMemory;
  bool send_evictions;

  MessageBuffer * requestToDir, network=&quot;To&quot;, virtual_network=&quot;0&quot;, vnet_type=&quot;request&quot;;
  MessageBuffer * responseToDirOrSibling, network=&quot;To&quot;, virtual_network=&quot;2&quot;, vnet_type=&quot;response&quot;;

  MessageBuffer * forwardFromDir, network=&quot;From&quot;, virtual_network=&quot;1&quot;, vnet_type=&quot;forward&quot;;
  MessageBuffer * responseFromDirOrSibling, network=&quot;From&quot;, virtual_network=&quot;2&quot;, vnet_type=&quot;response&quot;;

  MessageBuffer * mandatoryQueue;

{

}
</code></pre>
<p>We have five different message buffers: two &quot;To&quot;, two &quot;From&quot;, and one
special message buffer. The &quot;To&quot; message buffers are similar to slave
ports in gem5. These are the message buffers that this controller uses
to send messages to other controllers in the system. The &quot;From&quot; message
buffers are like slave ports. This controller receives messages on
&quot;From&quot; buffers from other controllers in the system.</p>
<p>We have two different &quot;To&quot; buffers, one for low priority requests, and
one for high priority responses. The priority for the networks are not
inherent. The priority is based on the order that other controllers look
at the message buffers. It is a good idea to number the virtual networks
so that higher numbers mean higher priority, but the virtual network
number is ignored by Ruby except that messages on network 2 can only go
to other message buffers on network 2 (i.e., messages can't jump from
one network to another).</p>
<p>Similarly, there is two different ways this cache can receive messages,
either as a forwarded request from the directory (e.g., another cache
requests a writable block and we have a readable copy) or as a response
to a request this controller made. The response is higher priority than
the forwarded requests.</p>
<p>Finally, there is a special message buffer, the <code>mandatoryQueue</code>. This
message buffer is used by the <code>Sequencer</code> to convert gem5 packets into
Ruby requests. Unlike the other message buffers, <code>mandatoryQueue</code> does
not connect to the Ruby network. Note: the name of this message buffer
is hard-coded and must be exactly &quot;mandatoryQueue&quot;.</p>
<p>As previously mentioned, this parameter block is converted into the
SimObject description file. Any parameters you put in this block will be
SimObject parameters that are accessible from the Python configuration
files. If you look at the generated file L1Cache_Controller.py, it will
look very familiar. Note: This is a generated file and you should never
modify generated files directly!</p>
<pre><code class="language-python">from m5.params import *
from m5.SimObject import SimObject
from Controller import RubyController

class L1Cache_Controller(RubyController):
    type = 'L1Cache_Controller'
    cxx_header = 'mem/protocol/L1Cache_Controller.hh'
    sequencer = Param.RubySequencer(&quot;&quot;)
    cacheMemory = Param.RubyCache(&quot;&quot;)
    send_evictions = Param.Bool(&quot;&quot;)
    requestToDir = Param.MessageBuffer(&quot;&quot;)
    responseToDirOrSibling = Param.MessageBuffer(&quot;&quot;)
    forwardFromDir = Param.MessageBuffer(&quot;&quot;)
    responseFromDirOrSibling = Param.MessageBuffer(&quot;&quot;)
    mandatoryQueue = Param.MessageBuffer(&quot;&quot;)
</code></pre>
<h2 id="state-declarations"><a class="header" href="#state-declarations">State declarations</a></h2>
<p>The next part of the state machine is the state declaration. Here, we
are going to declare all of the stable and transient states for the
state machine. We will follow the naming convention in Sorin et al. For
instance, the transient state &quot;IM_AD&quot; corresponds to moving from
Invalid to Modified waiting on acks and data. These states come directly
from the left column of Table 8.3 in Sorin et al.</p>
<pre><code class="language-cpp">state_declaration(State, desc=&quot;Cache states&quot;) {
    I,      AccessPermission:Invalid,
                desc=&quot;Not present/Invalid&quot;;

    // States moving out of I
    IS_D,   AccessPermission:Invalid,
                desc=&quot;Invalid, moving to S, waiting for data&quot;;
    IM_AD,  AccessPermission:Invalid,
                desc=&quot;Invalid, moving to M, waiting for acks and data&quot;;
    IM_A,   AccessPermission:Busy,
                desc=&quot;Invalid, moving to M, waiting for acks&quot;;

    S,      AccessPermission:Read_Only,
                desc=&quot;Shared. Read-only, other caches may have the block&quot;;

    // States moving out of S
    SM_AD,  AccessPermission:Read_Only,
                desc=&quot;Shared, moving to M, waiting for acks and 'data'&quot;;
    SM_A,   AccessPermission:Read_Only,
                desc=&quot;Shared, moving to M, waiting for acks&quot;;

    M,      AccessPermission:Read_Write,
                desc=&quot;Modified. Read &amp; write permissions. Owner of block&quot;;

    // States moving to Invalid
    MI_A,   AccessPermission:Busy,
                desc=&quot;Was modified, moving to I, waiting for put ack&quot;;
    SI_A,   AccessPermission:Busy,
                desc=&quot;Was shared, moving to I, waiting for put ack&quot;;
    II_A,   AccessPermission:Invalid,
                desc=&quot;Sent valid data before receiving put ack. &quot;Waiting for put ack.&quot;;
}
</code></pre>
<p>Each state has an associated access permission: &quot;Invalid&quot;, &quot;NotPresent&quot;,
&quot;Busy&quot;, &quot;Read_Only&quot;, or &quot;Read_Write&quot;. The access permission is used
for <em>functional</em> accesses to the cache. Functional accesses are
debug-like accesses when the simulator wants to read or update the data
immediately. One example of this is reading in files in SE mode which
are directly loaded into memory.</p>
<p>For functional accesses all caches are checked to see if they have a
corresponding block with matching address. For functional reads, <em>all</em>
of the blocks with a matching address that have read-only or read-write
permission are accessed (they should all have the same data). For
functional writes, all blocks are updated with new data if they have
busy, read-only, or read-write permission.</p>
<h2 id="event-declarations"><a class="header" href="#event-declarations">Event declarations</a></h2>
<p>Next, we need to declare all of the events that are triggered by
incoming messages for this cache controller. These events come directly
from the first row in Table 8.3 in Sorin et al.</p>
<pre><code class="language-cpp">enumeration(Event, desc=&quot;Cache events&quot;) {
    // From the processor/sequencer/mandatory queue
    Load,           desc=&quot;Load from processor&quot;;
    Store,          desc=&quot;Store from processor&quot;;

    // Internal event (only triggered from processor requests)
    Replacement,    desc=&quot;Triggered when block is chosen as victim&quot;;

    // Forwarded request from other cache via dir on the forward network
    FwdGetS,        desc=&quot;Directory sent us a request to satisfy GetS. We must have the block in M to respond to this.&quot;;
    FwdGetM,        desc=&quot;Directory sent us a request to satisfy GetM. We must have the block in M to respond to this.&quot;;
    Inv,            desc=&quot;Invalidate from the directory.&quot;;
    PutAck,         desc=&quot;Response from directory after we issue a put. This must be on the fwd network to avoid deadlock.&quot;;

    // Responses from directory
    DataDirNoAcks,  desc=&quot;Data from directory (acks = 0)&quot;;
    DataDirAcks,    desc=&quot;Data from directory (acks &gt; 0)&quot;;

    // Responses from other caches
    DataOwner,      desc=&quot;Data from owner&quot;;
    InvAck,         desc=&quot;Invalidation ack from other cache after Inv&quot;;

    // Special event to simplify implementation
    LastInvAck,     desc=&quot;Triggered after the last ack is received&quot;;
}
</code></pre>
<h2 id="user-defined-structures"><a class="header" href="#user-defined-structures">User-defined structures</a></h2>
<p>Next, we need to define some structures that we will use in other places
in this controller. The first one we will define is <code>Entry</code>. This is the
structure that is stored in the <code>CacheMemory</code>. It only needs to contain
data and a state, but it may contain any other data you want. Note: The
state that this structure is storing is the <code>State</code> type that was
defined above, not a hardcoded state type.</p>
<p>You can find the abstract version of this class (<code>AbstractCacheEntry</code>)
in <code>src/mem/ruby/slicc_interface/AbstractCacheEntry.hh</code>. If you want to
use any of the member functions of <code>AbstractCacheEntry</code>, you need to
declare them here (this isn't used in this protocol).</p>
<pre><code class="language-cpp">structure(Entry, desc=&quot;Cache entry&quot;, interface=&quot;AbstractCacheEntry&quot;) {
    State CacheState,        desc=&quot;cache state&quot;;
    DataBlock DataBlk,       desc=&quot;Data in the block&quot;;
}
</code></pre>
<p>Another structure we will need is a TBE. TBE is the &quot;transaction buffer
entry&quot;. This stores information needed during transient states. This is
<em>like</em> an MSHR. It functions as an MSHR in this protocol, but the entry
is also allocated for other uses. In this protocol, it will store the
state (usually needed), data (also usually needed), and the number of
acks that this block is currently waiting for. The <code>AcksOutstanding</code> is
used for the transitions where other controllers send acks instead of
the data.</p>
<pre><code class="language-cpp">structure(TBE, desc=&quot;Entry for transient requests&quot;) {
    State TBEState,         desc=&quot;State of block&quot;;
    DataBlock DataBlk,      desc=&quot;Data for the block. Needed for MI_A&quot;;
    int AcksOutstanding, default=0, desc=&quot;Number of acks left to receive.&quot;;
}
</code></pre>
<p>Next, we need a place to store all of the TBEs. This is an externally
defined class; it is defined in C++ outside of SLICC. Therefore, we need
to declare that we are going to use it, and also declare any of the
functions that we will call on it. You can find the code for the
<code>TBETable</code> in src/mem/ruby/structures/TBETable.hh. It is templatized on
the TBE structure defined above, which gets a little confusing, as we
will see.</p>
<pre><code class="language-cpp">structure(TBETable, external=&quot;yes&quot;) {
  TBE lookup(Addr);
  void allocate(Addr);
  void deallocate(Addr);
  bool isPresent(Addr);
}
</code></pre>
<p>The <code>external=&quot;yes&quot;</code> tells SLICC to not look for the definition of this
structure. This is similar to declaring a variable <code>extern</code> in C/C++.</p>
<h2 id="other-declarations-and-definitions-required"><a class="header" href="#other-declarations-and-definitions-required">Other declarations and definitions required</a></h2>
<p>Finally, we are going to go through some boilerplate of declaring
variables, declaring functions in <code>AbstractController</code> that we will use
in this controller, and defining abstract functions in
<code>AbstractController</code>.</p>
<p>First, we need to have a variable that stores a TBE table. We have to do
this in SLICC because it is not until this time that we know the true
type of the TBE table since the TBE type was defined above. This is some
particularly tricky (or nasty) code to get SLICC to generate the right
C++ code. The difficulty is that we want templatize <code>TBETable</code> based on
the <code>TBE</code> type above. The key is that SLICC mangles the names of all
types declared in the machine with the machine's name. For instance,
<code>TBE</code> is actually L1Cache_TBE in C++.</p>
<p>We also want to pass a parameter to the constructor of the <code>TBETable</code>.
This is a parameter that is actually part of the <code>AbstractController</code>,
thus we need to use the C++ name for the variable since it doesn't have
a SLICC name.</p>
<pre><code class="language-cpp">TBETable TBEs, template=&quot;&lt;L1Cache_TBE&gt;&quot;, constructor=&quot;m_number_of_TBEs&quot;;
</code></pre>
<p>If you can understand the above code, then you are an official SLICC
ninja!</p>
<p>Next, any functions that are part of AbstractController need to be
declared, if we are going to use them in the rest of the file. In this
case, we are only going to use <code>clockEdge()</code>:</p>
<pre><code class="language-cpp">Tick clockEdge();
</code></pre>
<p>There are a few other functions we're going to use in actions. These
functions are used in actions to set and unset implicit variables
available in action code-blocks. Action code blocks will be explained in
detail in the action section &lt;MSI-actions-section&gt;. These may be
needed when a transition has many actions.</p>
<pre><code class="language-cpp">void set_cache_entry(AbstractCacheEntry a);
void unset_cache_entry();
void set_tbe(TBE b);
void unset_tbe();
</code></pre>
<p>Another useful function is <code>mapAddressToMachine</code>. This allows us to
change the address mappings for banked directories or caches at runtime
so we don't have to hardcode them in the SLICC file.</p>
<pre><code class="language-cpp">MachineID mapAddressToMachine(Addr addr, MachineType mtype);
</code></pre>
<p>Finally, you can also add any functions you may want to use in the file
and implement them here. For instance, it is convenient to access cache
blocks by address with a single function. Again, in this function there
is some SLICC trickery. We need to access &quot;by pointer&quot; since the cache
block is something that we need to be mutable later (&quot;by reference&quot;
would have been a better name). The cast is also necessary since we
defined a specific <code>Entry</code> type in the file, but the <code>CacheMemory</code> holds
the abstract type.</p>
<pre><code class="language-cpp">// Convenience function to look up the cache entry.
// Needs a pointer so it will be a reference and can be updated in actions
Entry getCacheEntry(Addr address), return_by_pointer=&quot;yes&quot; {
    return static_cast(Entry, &quot;pointer&quot;, cacheMemory.lookup(address));
}
</code></pre>
<p>The next set of boilerplate code rarely changes between different
protocols. There's a set of functions that are pure-virtual in
<code>AbstractController</code> that we must implement.</p>
<p><code>getState</code>
:   Given a TBE, cache entry, and address return the state of the block.
This is called on the block to decide which transition to execute
when an event is triggered. Usually, you return the state in the TBE
or cache entry, whichever is valid.</p>
<p><code>setState</code>
:   Given a TBE, cache entry, and address make sure the state is set
correctly on the block. This is called at the end of the transition
to set the final state on the block.</p>
<p><code>getAccessPermission</code>
:   Get the access permission of a block. This is used during functional
access to decide whether or not to functionally access the block. It
is similar to <code>getState</code>, get the information from the TBE if valid,
cache entry, if valid, or the block is not present.</p>
<p><code>setAccessPermission</code>
:   Like <code>getAccessPermission</code>, but sets the permission.</p>
<p><code>functionalRead</code>
:   Functionally read the data. It is possible the TBE has more
up-to-date information, so check that first. Note: testAndRead/Write
defined in src/mem/ruby/slicc_interface/Util.hh</p>
<p><code>functionalWrite</code>
:   Functionally write the data. Similarly, you may need to update the
data in both the TBE and the cache entry.</p>
<pre><code class="language-cpp">State getState(TBE tbe, Entry cache_entry, Addr addr) {
    // The TBE state will override the state in cache memory, if valid
    if (is_valid(tbe)) { return tbe.TBEState; }
    // Next, if the cache entry is valid, it holds the state
    else if (is_valid(cache_entry)) { return cache_entry.CacheState; }
    // If the block isn't present, then it's state must be I.
    else { return State:I; }
}

void setState(TBE tbe, Entry cache_entry, Addr addr, State state) {
  if (is_valid(tbe)) { tbe.TBEState := state; }
  if (is_valid(cache_entry)) { cache_entry.CacheState := state; }
}

AccessPermission getAccessPermission(Addr addr) {
    TBE tbe := TBEs[addr];
    if(is_valid(tbe)) {
        return L1Cache_State_to_permission(tbe.TBEState);
    }

    Entry cache_entry := getCacheEntry(addr);
    if(is_valid(cache_entry)) {
        return L1Cache_State_to_permission(cache_entry.CacheState);
    }

    return AccessPermission:NotPresent;
}

void setAccessPermission(Entry cache_entry, Addr addr, State state) {
    if (is_valid(cache_entry)) {
        cache_entry.changePermission(L1Cache_State_to_permission(state));
    }
}

void functionalRead(Addr addr, Packet *pkt) {
    TBE tbe := TBEs[addr];
    if(is_valid(tbe)) {
        testAndRead(addr, tbe.DataBlk, pkt);
    } else {
        testAndRead(addr, getCacheEntry(addr).DataBlk, pkt);
    }
}

int functionalWrite(Addr addr, Packet *pkt) {
    int num_functional_writes := 0;

    TBE tbe := TBEs[addr];
    if(is_valid(tbe)) {
        num_functional_writes := num_functional_writes +
            testAndWrite(addr, tbe.DataBlk, pkt);
        return num_functional_writes;
    }

    num_functional_writes := num_functional_writes +
            testAndWrite(addr, getCacheEntry(addr).DataBlk, pkt);
    return num_functional_writes;
}
</code></pre>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                            <a rel="prev" href="../part3/part3_01_cache-intro.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>
                            <a rel="next" href="../part3/part3_03_cache-in-ports.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>
                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                    <a rel="prev" href="../part3/part3_01_cache-intro.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>
                    <a rel="next" href="../part3/part3_03_cache-in-ports.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>

        <script type="text/javascript">
            window.playground_copyable = true;
        </script>
        <script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
        <script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
        <script src="../book.js" type="text/javascript" charset="utf-8"></script>

        <!-- Custom JS scripts -->
    </body>
</html>
